home *** CD-ROM | disk | FTP | other *** search
/ 3D Game Programming All in One / 3D Game Programming All in One Disc.iso / 3D2E / RESOURCES / CH6 / EMAGA6 / control / client / misc / connection.cs < prev    next >
Text File  |  2006-07-02  |  5KB  |  155 lines

  1. //============================================================================
  2. // control/client/misc/connection.cs
  3. //
  4. // Copyright (c) 2003 Kenneth C. Finney
  5. //============================================================================
  6.  
  7. // Functions dealing with Connecting to a server
  8.  
  9.  
  10. //-----------------------------------------------------------------------------
  11. // Server Connection Error
  12. //-----------------------------------------------------------------------------
  13.  
  14. addMessageCallback( 'MsgConnectionError', handleConnectionErrorMessage );
  15.  
  16. function handleConnectionErrorMessage(%msgType, %msgString, %msgError)
  17. {
  18.    // On Connect the server transmits a message to display if there
  19.    // are any problems with the Connection.  Most Connection Errors
  20.    // are game version differences, so hopefully the server message
  21.    // will tell us where to get the latest version of the game.
  22.    $ServerConnectionErrorMessage = %msgError;
  23. }
  24.  
  25.  
  26. //----------------------------------------------------------------------------
  27. // GameConnection client callbacks
  28. //----------------------------------------------------------------------------
  29.  
  30.  
  31. function GameConnection::InitialControlSet(%this)
  32. //----------------------------------------------------------------------------
  33. // This callback is called directly from inside the Torque Engine
  34. // during server initialization.
  35. //----------------------------------------------------------------------------
  36. {
  37.    Echo ("Setting Initial Control Object");
  38.  
  39.    // The first control object has been set by the server
  40.    // and we are now ready to go.
  41.  
  42.    Canvas.SetContent(PlayerInterface);
  43. }
  44.  
  45.  
  46.  
  47. function GameConnection::setLagIcon(%this, %state)
  48. {
  49.  
  50. }
  51.  
  52. function GameConnection::onConnectionAccepted(%this)
  53. {
  54.  
  55. }
  56.  
  57. function GameConnection::onConnectionTimedOut(%this)
  58. {
  59.    // Called when an established Connection times out
  60.    disConnectedCleanup();
  61.    MessageBoxOK( "TIMED OUT", "The server Connection has timed out.");
  62. }
  63.  
  64. function GameConnection::onConnectionDropped(%this, %msg)
  65. {
  66.    // Established Connection was dropped by the server
  67.    disConnectedCleanup();
  68.    MessageBoxOK( "DISConnect", "The server has dropped the Connection: " @ %msg);
  69. }
  70.  
  71. function GameConnection::onConnectionError(%this, %msg)
  72. {
  73.    // General Connection Error, usually raised by ghosted objects
  74.    // initialization problems, such as missing files.  We'll display
  75.    // the server's Connection Error message.
  76.    disConnectedCleanup();
  77.    MessageBoxOK( "DISConnect", $ServerConnectionErrorMessage @ " (" @ %msg @ ")" );
  78. }
  79.  
  80.  
  81. //----------------------------------------------------------------------------
  82. // Connection Failed Events
  83. //----------------------------------------------------------------------------
  84.  
  85. function GameConnection::onConnectRequestRejected( %this, %msg )
  86. {
  87.    switch$(%msg)
  88.    {
  89.       case "CR_INVALID_PROTOCOL_VERSION":
  90.          %Error = "Incompatible protocol version: Your game version is not compatible with this server.";
  91.       case "CR_INVALID_Connect_PACKET":
  92.          %Error = "Internal Error: badly formed network packet";
  93.       case "CR_YOUAREBANNED":
  94.          %Error = "You are not allowed to play on this server.";
  95.       case "CR_SERVERFULL":
  96.          %Error = "This server is full.";
  97.       case "CHR_PASSWORD":
  98.          // XXX Should put up a password-entry dialog.
  99.          if ($Client::Password $= "")
  100.             MessageBoxOK( "REJECTED", "That server requires a password.");
  101.          else {
  102.             $Client::Password = "";
  103.             MessageBoxOK( "REJECTED", "That password is incorrect.");
  104.          }
  105.          return;
  106.       case "CHR_PROTOCOL":
  107.          %Error = "Incompatible protocol version: Your game version is not compatible with this server.";
  108.       case "CHR_CLASSCRC":
  109.          %Error = "Incompatible game classes: Your game version is not compatible with this server.";
  110.       case "CHR_INVALID_CHALLENGE_PACKET":
  111.          %Error = "Internal Error: Invalid server response packet";
  112.       default:
  113.          %Error = "Connection Error.  Please try another server.  Error code: (" @ %msg @ ")";
  114.    }
  115.    disConnectedCleanup();
  116.    MessageBoxOK( "REJECTED", %Error);
  117. }
  118.  
  119. function GameConnection::onConnectRequestTimedOut(%this)
  120. {
  121.    disConnectedCleanup();
  122.    MessageBoxOK( "TIMED OUT", "Your Connection to the server timed out." );
  123. }
  124.  
  125.  
  126. //-----------------------------------------------------------------------------
  127. // DisConnect
  128. //-----------------------------------------------------------------------------
  129.  
  130. function Disconnect()
  131. {
  132.    // Delete the Connection if it's still there.
  133.    if (IsObject(ServerConnection))
  134.       ServerConnection.delete();
  135.    disConnectedCleanup();
  136.  
  137.    // Call destroyServer in case we're hosting
  138.    destroyServer();
  139. }
  140.  
  141. function disConnectedCleanup()
  142. {
  143.    // Clear misc script stuff
  144.    HudMessageVector.clear();
  145.  
  146.  
  147.    // Back to the launch screen
  148.    Canvas.setContent(MenuScreen);
  149.  
  150.    // Dump anything we're not using
  151.    ClearTextureHolds();
  152.    PurgeResources();
  153. }
  154.  
  155.